/**
 * Created by gim on 14-2-11.
 */
var assert = require("assert");
var mpl = require('../lib/model.js');
var path = require('path');
var db = require('./db/config.js').db;
var async = require('async');

describe('loadModel by path :', function () {

    function test(done){
        var user = new User();
        user.name = 'username';
        user.password = 'pwd23';
        async.series([function (cb) {
            user.save(function (err, user) {
                cb(err, user);
            })
        }], function (err, result) {
            if (err != null) {
                throw err;
            }
            User.findById(result[0]._id, function (err, user) {
                assert.notEqual(user, null);
                assert.equal(user.name, 'username');
                assert.equal(user.password, 'pwd23');
                done();
            });
        });

    }

    it('has dynamic method', function (done) {
        mpl.loadModel(path.join(__dirname, './model/user.js'), {db: db});
        test(done);
    });

    it('skip db option,test db null error  ',function(done){
        global._options = undefined;
        try{
          mpl.loadModel(path.join(__dirname, './model/user.js'));
        }catch (err){
          assert.equal(err.message,'db cant be null')
        }
        done();
    });

    it('skip db option ,single file require ',function(done){
        //reset the option field
        global._options = {db: db};
        mpl.loadModel(path.join(__dirname, './model/user.js'));
        test(done);

    });
});